package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.SeartchRet
import client.comm.baoding.api.bean.Zcjl
import client.comm.baoding.databinding.LayoutSearchretitemBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.commlib.base.BasePagingAdapter
import client.comm.commlib.base.PagingViewHoder
import com.bumptech.glide.RequestManager
import com.google.gson.Gson


class SearchRetAdapter(val context: Context, val manager: RequestManager) :
    BasePagingAdapter<SeartchRet.Goods>(object : DiffUtil.ItemCallback<SeartchRet.Goods>() {
        val g = Gson()

        override fun areItemsTheSame(
            oldItem: SeartchRet.Goods,
            newItem: SeartchRet.Goods
        ): Boolean {
            return true
        }

        override fun areContentsTheSame(
            oldItem: SeartchRet.Goods,
            newItem: SeartchRet.Goods
        ): Boolean {
            return g.toJson(oldItem).equals(g.toJson(newItem))
        }
    }) {

    override fun onViewAttachedToWindow(holder: PagingViewHoder) {
        super.onViewAttachedToWindow(holder)
        val lp: ViewGroup.LayoutParams = holder.itemView.getLayoutParams()
        if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams
            && holder.getItemViewType() === 2
        ) {
            val p = lp as StaggeredGridLayoutManager.LayoutParams
            p.isFullSpan = true
        }
    }


    fun itemClick(bean: SeartchRet.Goods) {
        context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
        })
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {
        val binding = holder.binding as LayoutSearchretitemBinding

        val bean = t as SeartchRet.Goods
        binding.bean = bean
        manager.load(bean.goods_img)
            .into(binding.img)
        binding.event = this
    }

    override fun getItemLayout(): Int {
        return R.layout.layout_searchretitem
    }
}